package demo.practice.double_pointer;

public class P5 {

    public static void main(String[] args) {
        P5 p5 = new P5();
        p5.longestPalindrome("a");
    }

    public String longestPalindrome(String s) {
        char[] chars = s.toCharArray();

        int range = chars.length - 1;
        for (; range >= 0; range--) {
            for (int i = 0; i + range < chars.length; i++) {
                int j = i + range;
                if (check(chars, i, j)) {
                    char[] res = new char[j - i + 1];
                    System.arraycopy(chars, i, res, 0, j - i + 1);
                    return new String(res);
                }
            }
        }
        return null;
    }

    public boolean check(char[] chars, int i, int j) {

        while (i < j) {
            if (chars[i] != chars[j]) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}
